///////////////////////////////////////////////////////////////////////////////
/// @author  Aaron Wilhelm
/// @file    getopts.h
/// @brief   
///////////////////////////////////////////////////////////////////////////////

#include "getopts.h"


GetOpts::GetOpts():m_opts(NULL), m_opt_cnt(0)
{
}

GetOpts::~GetOpts()
{
}

bool GetOpts::set_opts
(
    Option * list, 
    unsigned int list_size, 
    unsigned int argc, 
    char **argv
)
{
    unsigned int j;
    std::string str;
    argv++;
    argc--;

    // Not sure whether I should copy the data or use what's already there
    m_opts = list;
    m_opt_cnt = list_size;

    for(j = 0; j < m_opt_cnt; j++)
    {
        list[j].set = false;
    }

    for(unsigned int arg_index = 0; arg_index < argc; ++arg_index)
    {
        str = argv[arg_index];

        // long option
        if( str.size() > 1 && str[0] == '-' && str[1] == '-' )
        {
            str = str.substr(2);

            if( str.size() == 0 )
            {
                return false;
            }

            /* Find the index of the type of argument */
            for(j = 0; j < m_opt_cnt; ++j)
            {
                if( m_opts[j].lng == str )
                {
                    m_opts[j].set = true;
                    break;
                }
            }

            if( j == m_opt_cnt )
            {
                return false;
            }

            /* Save the argument of the option
             * in a vector of strings
             */
            if ( m_opts[j].type != NONE )
            {
                if( arg_index + 1 < argc )
                {
                    ++arg_index;
                    str = argv[arg_index];
                    m_opts[j].value.push_back(str);
                }
                else
                {
                    return false;
                }
            }

        }
        // short option
        else if( str[0] == '-' )
        {
            str = str.substr(1);
            if( str.size() == 0 )
            {
                return false;
            }

            /* This is used to get options in the format
             * "-abc" (which is the same as "-a -b -c")
             */
            for(unsigned int arg_char = 0; arg_char < str.size(); ++arg_char)
            {
                for(j = 0; j < m_opt_cnt; ++j)
                {
                    if( m_opts[j].shrt[0] == str[arg_char] )
                    {
                        m_opts[j].set = true;

                        if( m_opts[j].type != NONE )
                        {
                            if( arg_index + 1 < argc )
                            {

                                arg_index++;
                                std::string tmp = argv[arg_index];
                                m_opts[j].value.push_back(tmp);
                            }
                            else
                            {
                                return false;
                            }
                        }//if( m_opts[j].type != NONE )
                        break;
                    }//if( m_opts[j].shrt[0] == str[arg_char] )
                }// for(j = 0; j < m_opt_cnt; ++j)

                if( j == m_opt_cnt )
                {

                    return false;
                }
            }// for(unsignedintarg_char = 0; arg_char<str.size();++arg_char)
        }//else if( str[0] == '-' )
        // no flag
        else
        {
        }

    }

    return true;
}
